package com.its.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.its.manager.mapper.*;
import com.its.manager.pojo.*;
import com.its.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author lenovo
* @description 针对表【gym_admin】的数据库操作Service实现
* @createDate 2025-04-15 18:05:38
*/
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {
    //声明mapper层属性
    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private AdminRoleMapper adminRoleMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    //新增管理员
    @Override
    public int addAdminService(Admin admin) {
        return adminMapper.insert(admin);
    }

    //修改管理员
    @Override
    public int updateAdminService(Admin admin) {
        return adminMapper.updateById(admin);
    }

    //删除管理员
    @Override
    public int deleteAdminService(Long aid) {
        return adminMapper.deleteById(aid);
    }

    //根据ID查询管理员及其角色及其权限信息
    @Override
    public Admin selectAdminByIdService(Long aid) {
        //查询管理员信息
        Admin admin = adminMapper.selectById(aid);

        /*//查询管理员的角色ID
        QueryWrapper<AdminRole> adminRoleQueryWrapper = new QueryWrapper<>();
        adminRoleQueryWrapper.eq("aid",aid).select("rid");
        //select rid from gym_admin_role where aid = #{aid}
        List<Object> rids = adminRoleMapper.selectObjs(adminRoleQueryWrapper);

        //查询角色信息
        List<Role> roles = roleMapper.selectBatchIds(rids.stream().map(obj -> (Long) obj).collect(Collectors.toList()));

        //查询权限信息
        roles.forEach(role -> {
            QueryWrapper<RolePermission> rolePermissionQueryMapper = new QueryWrapper<>();
            rolePermissionQueryMapper.eq("rid",role.getRid()).select("pid");
            //select pid from gym_role_permission where rid = #{rid}
            List<Object> pids = rolePermissionMapper.selectObjs(rolePermissionQueryMapper);

            //根据pid去查询权限信息
            List<Permission> permissions = permissionMapper.selectBatchIds(pids.stream().map(obj -> (Long) obj).collect(Collectors.toList()));

            //将权限信息存储到角色中
            role.setPermissions(permissions);
        });
        admin.setRoles(roles);*/
        return admin;
    }

    //分页查询管理员信息
    @Override
    public Page<Admin> selectAdminByPageService(Integer page, Integer size) {
        return adminMapper.selectPage(new Page<>(page,size),null);
    }

    //修改管理员角色信息
    @Transactional
    @Override
    public int updateAdminRoleService(Long aid, Long[] rids) {
       //修改--先删除，再增加
        int i = adminMapper.deleteById(new QueryWrapper<>().eq("aid", aid));
        //新增
        for (Long rid : rids) {
            AdminRole adminRole = new AdminRole();
            adminRole.setAid(aid);
            adminRole.setRid(rid);
            adminRoleMapper.insert(adminRole);
        }
        return i;
    }

    @Override
    public Admin getByUsername(String username) {
        return adminMapper.selectOne(new QueryWrapper<Admin>().eq("username", username));
    }
}




